from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.

# def index(requests):
#     return HttpResponse('hello.django,blog index')

from .models import Fenlei, Banner, WenZ, Biaoqian, Link
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger    # 分页插件


#首页
def index(request):
    all_fenlei = Fenlei.objects.all()   # 查询所有分类
    banner = Banner.objects.filter(is_active=True)[0:4]     # 查询所有激活后的幻灯片，并切片
    tui = WenZ.objects.filter(tui_id=1)[:3]     # 查询推荐位ID为1的文章
    allwenz = WenZ.objects.all().order_by('-id')[0:10]  #最新的10篇文章
    hot = WenZ.objects.all().order_by('-dianzan')[0:10]     #最多赞的10篇文章
    tui_right = WenZ.objects.filter(tui_id=2)[:6]   #推荐位ID为2的文章
    tags = Biaoqian.objects.all()
    link = Link.objects.all()




    # 把查询结果的分类封装到上下文
    context = {
        'all_fenlei': all_fenlei,
        'banner': banner,   # 把幻灯片数据封装到上下文
        'tui': tui,
        'allwenz': allwenz,
        'hot': hot,
        'tui_right': tui_right,
        'tags': tags,
        'link': link,
    }

    return render(request, 'index.html', context)   # 上下文传到index.html里面


#列表页
def list(request, lid):
    all_fenlei = Fenlei.objects.all()   # 查询所有分类
    list = WenZ.objects.filter(fenlei_id=lid)   # 获取url传进来的lid，然后查询出文章。
    fenlei_name = Fenlei.objects.get(id=lid)    # 获取当前文章的栏目名
    tui_right = WenZ.objects.filter(tui_id=2)[:6]   # 推荐位ID为2的文章
    tags = Biaoqian.objects.all()


    page = request.GET.get('page')  # 在url里获取页数
    paginator = Paginator(list, 5)  # 分页，超过5分页
    try:
        list = paginator.page(page)#获取当前页码的记录
    except PageNotAnInteger:
        list = paginator.page(1)#如果用户输入的页码不是整数时,显示第1页的内容
    except EmptyPage:
        list = paginator.page(paginator.num_pages)#如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容



    return render(request, 'list.html', locals())

#内容页
def show(request, sid):
    show = WenZ.objects.get(id=sid)     # 查询指定ID的文章
    all_fenlei = Fenlei.objects.all()   # 查询所有分类
    tags = Biaoqian.objects.all()
    tui_right = WenZ.objects.filter(tui_id=2)[:6]   # 推荐位ID为2的文章
    hot = WenZ.objects.all().order_by('?')[:10]     # 10篇随机文章
    befor_wenz = WenZ.objects.filter(created_time__gt=show.created_time, fenlei=show.fenlei.id).first()
    next_wenz = WenZ.objects.filter(created_time__lt=show.created_time, fenlei=show.fenlei.id).last()

    show.views = show.views + 1
    show.save()
    return render(request, 'show.html', locals())






#标签页面
def biaoqian(request, biaoqian):
    list = WenZ.objects.filter(biaoqian__name=biaoqian)     # 通过文章标签进行查询文章
    tui_right = WenZ.objects.filter(tui_id=2)[:6]   # 推荐位ID为2的文章
    all_fenlei = Fenlei.objects.all()  # 查询所有分类
    t_name = Biaoqian.objects.get(name=biaoqian)
    page = request.GET.get('page')
    tags = Biaoqian.objects.all()
    paginator = Paginator(list, 5)
    try:
        list = paginator.page(page)  # 获取当前页码的记录
    except PageNotAnInteger:
        list = paginator.page(1)  # 如果用户输入的页码不是整数时,显示第1页的内容
    except EmptyPage:
        list = paginator.page(paginator.num_pages)  # 如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容
    return render(request, 'tags.html', locals())


#搜索页面
def search(request):
    ss = request.GET.get('search')  # 获取搜索的关键词
    list = WenZ.objects.filter(title__icontains=ss)     # 关键词通过标题进行搜索
    tui_right = WenZ.objects.filter(tui_id=2)[:6]   # 推荐位ID为2的文章
    all_fenlei = Fenlei.objects.all()  # 查询所有分类
    page = request.GET.get('page')
    tags = Biaoqian.objects.all()
    paginator = Paginator(list, 10)
    try:
        list = paginator.page(page) # 获取当前页码的记录
    except PageNotAnInteger:
        list = paginator.page(1) # 如果用户输入的页码不是整数时,显示第1页的内容
    except EmptyPage:
        list = paginator.page(paginator.num_pages) # 如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容
    return render(request, 'search.html', locals())


#关于我们
def about(request):
    all_fenlei = Fenlei.objects.all()
    return render(request, 'page.html', locals())

def all(request):
    list = WenZ.objects.all().order_by('-id')     # 通过文章标签进行查询文章
    tui_right = WenZ.objects.filter(tui_id=2)[:6]   # 推荐位ID为2的文章
    all_fenlei = Fenlei.objects.all()  # 查询所有分类
    # t_name = Biaoqian.objects.get(name=biaoqian)
    page = request.GET.get('page')
    tags = Biaoqian.objects.all()
    paginator = Paginator(list, 5)
    try:
        list = paginator.page(page)  # 获取当前页码的记录
    except PageNotAnInteger:
        list = paginator.page(1)  # 如果用户输入的页码不是整数时,显示第1页的内容
    except EmptyPage:
        list = paginator.page(paginator.num_pages)  # 如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容
    return render(request, 'all.html', locals())

def pageNotfound(request):
    # 404
    all_fenlei = Fenlei.objects.all()
    return render(request, '404.html', locals())
